<?php
/**
 * [JishiGou] (C)2005 - 2099 Cenwor Inc.
 *
 * This is NOT a freeware, use is subject to license terms
 *
 * @Filename navigation.mod.php $
 *
 * @Author http://www.jishigou.net $
 *
 * @Date 2013-05-21 17:04:54 606829612 2024841034 13342 $
 */




if (!defined('IN_JISHIGOU')) {
    exit('invalid request');
}

class ModuleObject extends MasterObject {

    var $WLogic;

    public function ModuleObject($config) {
        $this->MasterObject($config);
        $code = $this->Code;

        ob_start();
        if ($code && method_exists($this, $code)) {
            $this->$code();
        } else {
            $this->navigation('top');
        }
		$this->ShowBody(ob_get_clean());
    }

    
    public function navigation($local = 'top') {
        $conf_file = 'navigation';
        if ($local == "footer") {
            $conf_file = "footer_navigation";
        }
        $slide_config = ConfigHandler::get($conf_file);
        $slide_list = $slide_config['list'];
                foreach ($slide_list as $group_key => $group_one) {
            if ($group_one['code'] == 'beian') {
                $slide_list[$group_key]['name'] = $this->Config['icp'];
            }
            foreach ($group_one['type_list'] as $item_key => $item_one) {
                if ($item_one['code'] == 'beian') {
                    $slide_list[$group_key]['type_list'][$item_key]['name'] = $this->Config['icp'];
                }
            }
        }
                include(template('admin/navigation_navigation'));
    }

    
    public function footer_navigation() {
        $this->navigation('footer');
    }

    
    public function save() {
        if ($this->Post['local'] == 'footer') {
            $conf_file = 'footer_navigation';
        } elseif ($this->Post['local'] == 'top') {
            $conf_file = 'navigation';
        }
                $conf_file || $this->Messager('请重试！');

        $nav_old_config = ConfigHandler::get($conf_file);

        $nav_conf = jget('slide');
        $del = jget('del');
        $del_one = jget('del_one');
        $slide_new_order = jget('slide_new_order');
        $slide_new_name = jget('slide_new_name');
        $slide_new_code = jget('slide_new_code');
        $slide_new_url = jget('slide_new_url');
        $slide_new_target = jget('slide_new_target');
        $slide_new_avaliable = jget('slide_new_avaliable');


        $type_new_order = jget('type_new_order');
        $type_new_name = jget('type_new_name');
        $type_new_code = jget('type_new_code');
        $type_new_url = jget('type_new_url');
        $type_new_target = jget('type_new_target');
        $type_new_avaliable = jget('type_new_avaliable');
                if (is_array($slide_new_name)) {
            foreach ($slide_new_name as $new_group_key => $new_group) {
                $nav_conf[] = array(
                    'order' => (int) $slide_new_order[$new_group_key],
                    'name' => $slide_new_name[$new_group_key],
                    'code' => $slide_new_code[$new_group_key],
                    'url' => $slide_new_url[$new_group_key],
                    'target' => $slide_new_target[$new_group_key],
                    'avaliable' => (int) $slide_new_avaliable[$new_group_key],
                );
            }
        }
                if (is_array($type_new_name)) {
            foreach ($type_new_name as $new_key => $item) {
                if ($nav_conf[$new_key]) {
                    foreach ($item as $new_one_key => $one) {
                        if ($one) {
                            $nav_conf[$new_key]['type_list'][] = array(
                                'order' => (int) $type_new_order[$new_key][$new_one_key],
                                'name' => $type_new_name[$new_key][$new_one_key],
                                'code' => $type_new_code[$new_key][$new_one_key],
                                'url' => $type_new_url[$new_key][$new_one_key],
                                'target' => $type_new_target[$new_key][$new_one_key],
                                'avaliable' => (int) $type_new_avaliable[$new_key][$new_one_key],
                            );
                        }
                    }
                }
            }
        }
                if (is_array($del)) {
            foreach ($del as $one) {
                if ($nav_conf[$one]) {
                    unset($nav_conf[$one]);
                }
            }
        }
                if (is_array($del_one)) {
            foreach ($del_one as $one_key => $group) {
                if ($nav_conf[$one_key]['type_list']) {
                    foreach ($group as $one) {
                        if ($nav_conf[$one_key]['type_list'][$one]) {
                            unset($nav_conf[$one_key]['type_list'][$one]);
                        }
                    }
                }
            }
        }

                foreach ($nav_conf as $key_for_dort_g => $group_temp) {
                        foreach ($group_temp['type_list'] as $item_key => $item_one) {
                if ($item_one['code'] == 'beian') {
                    $group_temp['type_list'][$item_key]['name'] = $this->Config['icp'];
                }
            }
                        if ($group_temp['type_list']) {
                usort($group_temp['type_list'], create_function('$a,$b', 'if($a[order]==$b[order])return 0;return $a[order]<$b[order]?1:-1;'));
            }
            $nav_conf[$key_for_dort_g]['type_list'] = $group_temp['type_list'];
        }

        if ($nav_conf) {
            usort($nav_conf, create_function('$a,$b', 'if($a[order]==$b[order])return 0;return $a[order]<$b[order]?1:-1;'));
        }
                if (!$nav_conf) {
            $nav_conf = $nav_old_config;
        }
        $slide['list'] = $nav_conf;
                $slide['pluginmenu'] = $nav_old_config['pluginmenu'] ? $nav_old_config['pluginmenu'] : array();
        $r = ConfigHandler::set($conf_file, $slide);
        $this->Messager('设置成功');
    }

    
    public function left_navigation() {

        $slide_list = ConfigHandler::get('left_navigation');

        include(template('admin/navigation_left_navigation'));
    }

    
    public function modify_left_icon() {

        $key = $this->Get['key'];
        $group = $this->Get['group'];
        $name = $this->Get['name'];

        Load::lib('image');
        $image = new image();
        Load::lib('upload');
        $file = $key;

        $slide = $this->Post['slide'];

        if ($_FILES[$file]['name']) {

            
            $image_name = $key . ".jpg";
            $image_path = RELATIVE_ROOT_PATH . 'images/lefticon/';
            $image_file = $image_path . $image_name;

            if (!is_dir($image_path)) {
                jlib('io')->MakeDir($image_path);
            }

            $UploadHandler = new UploadHandler($image_path, $file, true);
            $UploadHandler->setMaxSize(512);
            $UploadHandler->setNewName($image_name);
            $result = $UploadHandler->doUpload();

            if ($result) {
                $result = is_image($image_file);
            }
            if (!$result) {
                unlink($image_file);
                echo "<script type='text/javascript'>";
                echo "parent.document.getElementById('message').style.display='none';";
                echo "</script>";
                $this->_image_error($UploadHandler->_error);
                exit();
            }
            $image->Thumb($image_file, $image_file, 100, 100);
            if ($this->Config['ftp_on']) {
                $ftp_key = randgetftp();
                $get_ftps = ConfigHandler::get('ftp');
                $site_url = $get_ftps[$ftp_key]['attachurl'];
                $ftp_result = ftpcmd('upload', $image_file, '', $ftp_key);
                if ($ftp_result > 0) {
                    jlib('io')->DeleteFile($image_file);
                    $image_file = $site_url . '/' . str_replace('./', '', $image_file);
                }
            }
        } else {
            echo "<script type='text/javascript'>";
            echo "alert('没有图片');";
            echo "</script>";
            exit();
        }
                $slide_config = ConfigHandler::get('left_navigation');
        foreach ($slide_config[$group] as &$v) {
            if ($v['code'] == $name) {
                $v['icon'] = $image_file;
            }
        }
        ConfigHandler::set('left_navigation', $slide_config);

        echo "<script type='text/javascript'>";

                echo "parent.document.getElementById('show_image_$key').src='{$image_file}';";
        echo "parent.document.getElementById('show_image_$key').style.display='block';";
        echo "parent.document.getElementById('show_image_{$key}_value').value='{$image_file}';";
                echo "parent.document.location.reload();";
        echo "</script>";
        exit;
    }

    
    public function do_left_navigation() {
        $slide_config = ConfigHandler::get('left_navigation');
        $slide = $this->Post['slide'];
        $chk = $this->Post['chk'];
        $temp['order'] = $this->Post['slide_new_order'];
        $temp['name'] = $this->Post['slide_new_name'];
        $temp['code'] = $this->Post['slide_new_code'];
        $temp['url'] = $this->Post['slide_new_url'];
        $temp['avaliable'] = $this->Post['slide_new_avaliable'];
        $temp_for_slide = array("mine" => array(), "myapp" => array(), "app" => array());
        if ($slide) {

            foreach ($temp_for_slide as $k_type => $value) {

                                if (!empty($temp['code'][$k_type])) {
                    foreach ($temp['code'][$k_type] as $k_new_one => $code_one) {
                        if (!$code_one || !$temp['name'][$k_type][$k_new_one]) {
                                                        continue;
                        }
                                                $slide[$k_type][$code_one]['code'] = $code_one;
                        $slide[$k_type][$code_one]['name'] = $temp['name'][$k_type][$k_new_one];
                        $slide[$k_type][$code_one]['order'] = $temp['order'][$k_type][$k_new_one];
                        $slide[$k_type][$code_one]['url'] = $temp['url'][$k_type][$k_new_one];
                        $slide[$k_type][$code_one]['avaliable'] = $temp['avaliable'][$k_type][$k_new_one];
                    }
                }

                foreach ($slide[$k_type] as $k => $v) {
                    if ($chk && in_array($k, $chk)) {
                                                unset($slide[$k_type][$k]);
                        continue;
                    }
                                        $slide[$k_type][$k]['avaliable'] = $v['avaliable'] ? 1 : 0;
                    if (!$v['name'] || !$v['code']) {
                        unset($slide[$k_type][$k]);
                        continue;
                    }
                                        $changeWords = array('mygroup');

                    if (in_array($v['code'], $changeWords)) {
                        $slide[$k_type][$k]['name'] = str_replace('圈子', $this->Config['changeword']['weiqun'], $v['name']);
                    }
                                        $changeWords = array('newdigout', 'mydigout');
                    if (in_array($v['code'], $changeWords)) {
                        $slide[$k_type][$k]['name'] = str_replace('赞', $this->Config['changeword']['dig'], $v['name']);
                    }
                }
            }
        }

                if ($slide['mine']) {
            usort($slide['mine'], create_function('$a,$b', 'if($a[order]==$b[order])return 0;return $a[order]<$b[order]?1:-1;'));
        } else {
            $slide['mine'] = array();
        }
        if ($slide['myapp']) {
            usort($slide['myapp'], create_function('$a,$b', 'if($a[order]==$b[order])return 0;return $a[order]<$b[order]?1:-1;'));
        } else {
            $slide['myapp'] = array();
        }
        if ($slide['app']) {
            usort($slide['app'], create_function('$a,$b', 'if($a[order]==$b[order])return 0;return $a[order]<$b[order]?1:-1;'));
        } else {
            $slide['app'] = array();
        }


        $slide = $slide ? $slide : $slide_config;
        ConfigHandler::set('left_navigation', $slide);
        $this->Messager('设置成功');
    }

}

?>
